package com.example.final_project.Home.Activity;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.ejlchina.okhttps.OkHttps;
import com.example.final_project.Home.entry.CarNews;
import com.example.final_project.Internet.Internet;
import com.example.final_project.MainView;
import com.example.final_project.R;

import java.util.List;

public class CarNewsActivity extends AppCompatActivity {

    private Context context;
    private String name;
    private ImageView back,img1,img2;
    private TextView title,author,content;
    public static final int INIT=0;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_car_news);

        Intent intent = this.getIntent();
        name = intent.getStringExtra("name");
        findById();
        getData();

        //设置返回点击事件
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(CarNewsActivity.this, MainView.class);
                intent.setAction("backToHome");
                startActivity(intent);
            }
        });

    }

    private Handler handler = new Handler(Looper.getMainLooper()){
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            switch (msg.what){
                case INIT:
                    List<CarNews> list = (List<CarNews>)msg.obj;
                    CarNews carNews = list.get(0);
                    title.setText(carNews.getTitle());
                    author.setText(carNews.getAuthor());
                    content.setText(carNews.getContent());
                    Glide.with(context).load("http://10.150.11.9:8086/chengcar/banners/"+carNews.getImg1()).into(img1);
                    Glide.with(context).load("http://10.150.11.9:8086/chengcar/banners/"+carNews.getImg2()).into(img2);
                    break;
            }
        }
    };

    public void getData(){
        OkHttps.async("http://10.150.11.9:8086/chengcar/car")
                .bind(context)
                .addUrlPara("name",name)
                .setOnResList(CarNews.class,news ->{
                    Message message = Message.obtain();
                    message.what=INIT;
                    message.obj=news;
                    handler.sendMessage(message);
                })
                .get();
//        Internet internet = Internet.getInstance();
//        internet.getCarNews(name,context);
    }

    public void findById(){
        context = getBaseContext();
        back = this.findViewById(R.id.Cnews_back);
        img1 = this.findViewById(R.id.Cnews_img1);
        img2 = this.findViewById(R.id.Cnews_img2);
        title = this.findViewById(R.id.Cnews_title);
        author = this.findViewById(R.id.Cnews_author);
        content = this.findViewById(R.id.Cnews_content);
    }

}